package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AppointmentServletImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Override
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Appointment appointment) {
        //生产预约单号
        //设置创建时间
        Date createTime = new Date();
        appointment.setAno(DateUtil.format(createTime,"yyyyMMddHHmmss") + RandomUtil.randomNumbers(5));
        appointment.setCreateTime(createTime);
        appointmentMapper.insert(appointment);

    }

    @Override
    public Appointment get(Long id) {
        return  appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Appointment> listAll() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);

    }

    @Override
    public PageInfo<Appointment> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"a.status,a.appointment_time");
           List<Appointment> appointments = appointmentMapper.selectForList(qo);
        return new PageInfo(appointments);
    }

    @Override
    public void updateStatus(Appointment appointment) {
        appointmentMapper.updateStatus(appointment);
    }


}
